//
//  FastFourierTransform.h
//  note-recog
//
//  Created by Philip Mulcahy on 02/02/2012.
//  Copyright (c) 2012 Philip Mulcahy. All rights reserved.
//

#ifndef note_recog_FastFourierTransform_h
#define note_recog_FastFourierTransform_h

#include <vector>
#include <AudioToolbox/AudioToolbox.h>
#include <CoreAudio/CoreAudioTypes.h>
#include <Accelerate/Accelerate.h>

class Spectrum;
class FastFourierTransform;

class FastFourierTransform
{
    
public:
    class Functor
    {
    private:
        mutable std::vector<short> _staging;
        FastFourierTransform * const _transform;
    public:
        explicit Functor(float samplingFreq);
        ~Functor();
        Spectrum operator()(const std::vector<short>& time_domain) const;
    };
    
public:    
    FastFourierTransform(const std::vector<short> & inputTimeDomain,
                         float samplingFreq);
    
    ~FastFourierTransform();
    
    Spectrum calculate();
    
private:
    const std::vector<short>& _inputTimeDomain;
    const float _samplingFreq;
    const size_t _frameCount;
    std::vector<float> _inOrderFloats;
    const size_t _log2frameCount;
    COMPLEX_SPLIT _cplx;
    std::vector<float> _freqDomainAbs;
    const FFTSetup _fftSetup;
    
    std::vector<float>::const_iterator abs_begin() const { return _freqDomainAbs.begin(); }
    std::vector<float>::const_iterator abs_end() const { return _freqDomainAbs.end(); }
};

#endif
